﻿using System;
using DBServer.Selenium.Extenium.Exception;
using DBServer.Selenium.Extenium.IFixture;
using DBServer.Selenium.Extenium.Path;
using Selenium;

namespace DBServer.Selenium.Extenium.Fixtures
{
    public class CheckRowGridFixture : RowFixture,ICheckBox
    {


        private readonly ISelenium _driver;
        private readonly string _name;
        private readonly Int32 _indexOfColumn;
        private readonly Int32 _indexOfRow;

        public CheckRowGridFixture(ISelenium driver, string name,Int32 indexOfRow, Int32 indexOfColumn)
            :base(driver,name,indexOfRow)
        {
            _driver = driver;
            _name = name;
            _indexOfRow = indexOfRow;
            _indexOfColumn = indexOfColumn;
        }


        public ICheckBox RequireEnabled()
        {
            var typeOfColumn = SafeExec(Path.Eval() + ".getView().cm.getColumnAt("+_indexOfColumn+").xtype");
            if(typeOfColumn != "checkcolumn")
            {
                throw new ExteniumException("The column don't contain a CheckBox");
            }
            return this;
        }

        public ICheckBox RequireDisabled()
        {
            var typeOfColumn = SafeExec(Path.Eval() + ".getView().cm.getColumnAt(" + _indexOfColumn + ").xtype");
            if (typeOfColumn == "checkcolumn")
            {
                throw new ExteniumException("The column don't contain a CheckBox");
            }
            return this;
        }

        public ICheckBox Check()
        {
            RequireUncheck();
            var _checked = SafeExec(Path.Eval() + ".store.data.items[" + _indexOfRow + "].set('" + getNameOfColumn(_indexOfColumn) + "', true);").ToLower();
            return RequireCheck();
        }

        public ICheckBox UnCheck()
        {
            RequireCheck();
            var _checked = SafeExec(Path.Eval() + ".store.data.items[" + _indexOfRow + "].set('" + getNameOfColumn(_indexOfColumn) + "', false);").ToLower();
            return RequireUncheck();
        }

        public ICheckBox RequireCheck()
        {
            var _checked = SafeExec(Path.Eval() + ".store.data.items[" + _indexOfRow + "].get('" + getNameOfColumn(_indexOfColumn) + "');").ToLower();
            if (_checked == "true")
            {
                return this;
            }
            throw new ExteniumException("CheckBox is not checked or it not exists");
        }

        public ICheckBox RequireUncheck()
        {
            var _checked = SafeExec(Path.Eval() + ".store.data.items[" + _indexOfRow + "].get('" + getNameOfColumn(_indexOfColumn) + "');").ToLower();
            if (_checked == "false")
            {
                return this;
            }
            throw new ExteniumException("CheckBox is checked or it not exists");
        }


        private string getNameOfColumn(int indexOfColumn)
        {
            var nameOfColumn = SafeExec(Path.Eval() + ".colModel.columns["+indexOfColumn+"].dataIndex");
            return nameOfColumn;
        }
    }
}